package com.smartlink.app.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.smartlink.app.R;
import com.smartlink.app.controller.LoginController;

public class LoginActivity extends AppCompatActivity {

    // Content View Elements

    private EditText et_login_number;
    private EditText et_login_password;
    private Button bt_login, btn_login_ip;
    private ProgressBar progressBar;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String body = (String) msg.obj;
            bt_login.setEnabled(true);
            progressBar.setVisibility(View.INVISIBLE);
            Toast.makeText(LoginActivity.this, body, Toast.LENGTH_SHORT).show();

            if (msg.what == 0x123) {
                LoginActivity.this.finish();
            }
            super.handleMessage(msg);
        }
    };


    // End Of Content View Elements

    private void bindViews() {

        et_login_number = (EditText) findViewById(R.id.et_login_number);
        et_login_password = (EditText) findViewById(R.id.et_login_password);
        bt_login = (Button) findViewById(R.id.bt_login);
        btn_login_ip = (Button) findViewById(R.id.btn_login_ip);
        progressBar = (ProgressBar) findViewById(R.id.progressBar_login);

        btn_login_ip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, SetIpActivity.class);
                startActivity(intent);
            }
        });

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        bindViews();
        Intent intent = getIntent();
        if (intent != null) {
            String error = intent.getStringExtra("error");
            if (!"".equals(error)) {
                Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
            }

        }
    }

    public void login(View view) {
        String number = et_login_number.getText().toString();
        String password = et_login_password.getText().toString();
        if (TextUtils.isEmpty(number)) {
            Toast.makeText(this, "请输入学号/工号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        LoginController loginController = new LoginController(this);
        loginController.login(number, password, handler);

        //设置登陆按钮不可用
        bt_login.setEnabled(false);
        progressBar.setVisibility(View.VISIBLE);

    }

}
